package com.hqyj.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.goods.dto.InventoryInfo;
import com.hqyj.storage.dto.StorageInfo;
import com.hqyj.storage.entity.Storage;
import com.hqyj.storage.mapper.StorageMapper;
import com.hqyj.storage.service.StorageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.suppliers.entity.Suppliers;
import com.hqyj.utils.PageUtil;
import com.hqyj.utils.ResultUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sdx2009
 * @since 2022-09-30
 */
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements StorageService {

    @Override
    public ResultUtils storageIndex(StorageInfo storageInfo) {
        Page<Storage> page = new Page<>(storageInfo.getPage(),storageInfo.getRow());
        QueryWrapper<Storage> qw = new QueryWrapper<>();
        qw.like(storageInfo.getStorageAddr() != null && !storageInfo.getStorageAddr().equals(""),"storage_addr",storageInfo.getStorageAddr());
        qw.like(storageInfo.getStorageType() != null && !storageInfo.getStorageType().equals(""),"storage_type",storageInfo.getStorageType());
        Page<Storage> pageList = this.page(page, qw);
        PageUtil<Storage> pageUtil = new PageUtil<>();
        pageUtil.setData(pageList.getRecords());
        pageUtil.setTotal(pageList.getTotal());
        pageUtil.setTotalPage(pageList.getPages());
        return new ResultUtils(200,"",pageUtil);
    }

    @Override
    public ResultUtils selectStorage() {
        return new ResultUtils(200,"",this.list());
    }

    @Override
    public ResultUtils storageUpdate(StorageInfo storageInfo) {
        UpdateWrapper<Storage> updateWrapper = new UpdateWrapper<>();
        Storage storage = new Storage();
        BeanUtils.copyProperties(storageInfo,storage);
        updateWrapper.eq("storage_id",storageInfo.getStorageId());
        if (this.update(storage,updateWrapper)){
            return new ResultUtils(200,"修改成功！",null);
        }
        return new ResultUtils(500,"修改失败！",null);
    }

    @Override
    public ResultUtils addStorage(StorageInfo storageInfo) {
        Storage storage = new Storage();
        BeanUtils.copyProperties(storageInfo,storage);
        if (this.save(storage)){
            return new ResultUtils(200,"添加成功！",null);
        }
        return new ResultUtils(500,"添加失败！",null);
    }

    @Override
    public ResultUtils deleteStorage(StorageInfo storageInfo) {
        if (this.removeById(storageInfo.getStorageId())){
            return new ResultUtils(200,"删除成功！",null);
        }
        return new ResultUtils(500,"删除失败！",null);
    }

    @Override
    public ResultUtils selectStorageByType(String type) {
        QueryWrapper<Storage> qw = new QueryWrapper<>();
        qw.eq("storage_type",type);
        return new ResultUtils(200,"",this.list(qw));
    }


}
